package com.sx.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.dfec.cache.redis.UrlDataCash;
import com.dfec.model.Msg;
import com.dfec.utils.Consists;
import com.dfec.utils.PropertiesUtil;
import com.dfec.utils.ResponseUtil;
import com.sx.service.AppVersionService;

/**
 * Servlet implementation class ForcastController
 */
@WebServlet(name = "ver", description = "app版本", urlPatterns = { "/ver" })
public class AppVersionController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	AppVersionService s=new AppVersionService();
	private List<Object> list=null;
    Msg msg=null;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		msg=new Msg();
		list=new ArrayList<>();
	
		String appname = request.getParameter("code_name");
		try {
				List<Object> getdata=s.getVersion(appname);
				if(!getdata.get(0).toString().equals("[]")){
					msg.setCode(Consists.DATA_Code_OK);
					msg.setMsg(Consists.DATA_Msg_OK);
				
				}else {
					msg.setCode(Consists.DATA_Code_OK__NODATA);
					msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
				}
				list.add(msg);
				list.add(getdata.get(0));
				
				
		} catch (Exception e) {
			e.printStackTrace();
			msg.setCode(Consists.Error_Code_ServerError);
			msg.setMsg(Consists.Error_Msg_ServerError);
			list.add(msg);
			list.add(new ArrayList<>());
		}
		if(msg.getCode().equals(Consists.DATA_Code_OK)&&PropertiesUtil.config_redis_use) {
			UrlDataCash.saveUrlData(UrlDataCash.urlKey(request).toString(), JSON.toJSONString(list));
		}
		 ResponseUtil.sendScript(response, JSON.toJSONString(list));
		
	}
}